<template>
  <div>
  	<el-dialog title="增加库存" :visible.sync="editorVisible" width="30%" :before-close="closeEditorStock" @open="opendEnd">
  		<el-form :model="addStock" ref="updateStock" label-width="100px">
        <el-form-item label="商品名称" prop="goodsName">
          <el-input
            placeholder="你要看到这个就说明数据加载没有成功"
            v-model="addStock.goodsName"
            :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="原库存数量" prop="stockNum">
          <el-input
            placeholder="这是一个文本框"
            v-model="addStock.stockNum"
            :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="库存数量" prop="addStockNum">
          <el-input-number v-model="addStock.addStockNum" controls-position="right" :min="0" :max="20"></el-input-number>
        </el-form-item>
  		</el-form>
  		<span slot="footer" class="dialog-footer">
  			<el-button @click="closeEditorStock()">取 消</el-button>
  			<el-button type="primary" @click="updateStock('updateStock')">确 定</el-button>
  		</span>
  	</el-dialog>
  </div>
</template>

<script>
  export default {
    name: "",
    props: ["editorVisible","editorObj"],
    data() {
    	return {
    		addStock: {
          goodsId:"",
          goodsName:"",
          stockNum:0,
          addStockNum:0
    		}
    	}
    },
    methods: {
      opendEnd(){
        this.addStock.goodsId = this.$props.editorObj.goodsId
        this.addStock.goodsName = this.$props.editorObj.goodsName
        this.addStock.stockNum = this.$props.editorObj.stockNum
      },
      //清除文本框方法
      clearData(){
        this.addStock= {
          goodsId:"",
          goodsName:"",
          stockNum:0,
          addStockNum:0
        }
      },
      //关闭对话框方法
      closeEditorStock(){
        this.clearData();
        this.$parent.editorVisible = false;
      },

      //添加方法
      updateStock(params){
        var self = this;
        this.$refs[params].validate((valid) => {
          if (valid) {
            var manager = JSON.parse(sessionStorage.getItem("manager"));
            //执行提交后台方法
            if(self.addStock.addStockNum!='' && self.addStock.addStockNum>0){
              self.uploadMethod(manager);
            }else{
              console.log('方法未执行')
              self.clearData()
              //关闭修改界面
              self.closeEditorStock()
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
      //提交后台方法
      uploadMethod(manager){
        var self = this;
        console.log(self.addStock.goodsId)
        console.log(self.addStock.addStockNum)
        self.$http({
            url:"/api/CakeShop/updateStock",
            method:"post",
            headers:{
              token:manager.token
            },
            data:{
              goodsId: self.addStock.goodsId,
              stockNum: self.addStock.addStockNum
            }
        }).then(function(res){
          console.log(res)
          if(res.data.code!=200){
            self.$message({
              message: res.data.msg,
              type: 'error',
            });
          }else{
            self.$message({
              message: res.data.msg,
              type: 'success',
              onClose:function(){
                self.clearData()
                //关闭修改界面
                self.closeEditorStock()
                //根据修改数据返回当前页
                self.$parent.queryStockList(sessionStorage.getItem("page"))
              }
            });
          }
        })
      }
    },
  }
</script>

<style>
</style>
